home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2001 December / pcwk12201b.iso / Wersje pelne i specjalne / Winamp 2.77 i 3.0beta / wasabi-sdk_beta1.exe / jnetlib / test.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  12.2 KB  |  514 lines

  1. /*
  2.  
  3.   Nullsoft WASABI Source File License
  4.  
  5.   Copyright 1999-2001 Nullsoft, Inc.
  6.  
  7.     This software is provided 'as-is', without any express or implied
  8.     warranty.  In no event will the authors be held liable for any damages
  9.     arising from the use of this software.
  10.  
  11.     Permission is granted to anyone to use this software for any purpose,
  12.     including commercial applications, and to alter it and redistribute it
  13.     freely, subject to the following restrictions:
  14.  
  15.     1. The origin of this software must not be misrepresented; you must not
  16.        claim that you wrote the original software. If you use this software
  17.        in a product, an acknowledgment in the product documentation would be
  18.        appreciated but is not required.
  19.     2. Altered source versions must be plainly marked as such, and must not be
  20.        misrepresented as being the original software.
  21.     3. This notice may not be removed or altered from any source distribution.
  22.  
  23.  
  24.   Brennan Underwood
  25.   brennan@nullsoft.com
  26.  
  27. */
  28.  
  29. /*
  30. ** JNetLib
  31. ** Copyright (C) 2000-2001 Nullsoft, Inc.
  32. ** Author: Justin Frankel
  33. ** File: test.cpp - JNL test code
  34. ** License: see jnetlib.h
  35. */
  36.  
  37. #ifdef _WIN32
  38. #include <windows.h>
  39. #else
  40. #define Sleep(x) usleep((x)*1000)
  41. #endif
  42. #include <stdio.h>
  43. #include "jnetlib.h"
  44.  
  45. #include "udpconnection.h"
  46.  
  47. #define TEST_ASYNCDNS 0
  48. #define TEST_CONNECTION 0
  49. #define TEST_LISTEN 0
  50. #define TEST_TELNET_GATEWAY 0
  51. #define TEST_HTTPGET 0
  52. #define TEST_UDP 1
  53.  
  54.  
  55. #if (TEST_HTTPGET)
  56. int main(int argc, char **argv)
  57. {
  58.  
  59.   if (argc != 3)
  60.   {
  61.     printf("usage: httpget <url> <outfile>\n");
  62.     exit(0);
  63.   }
  64.  
  65.   JNL_HTTPGet get;
  66.   JNL::open_socketlib();
  67.  
  68.   get.addheader("User-Agent:PooHead (Mozilla)");
  69.   get.addheader("Accept:*/*");
  70.   get.connect(argv[1]);
  71.  
  72.   FILE *fp=fopen(argv[2],"wb");
  73.   int headerstate=0;
  74.   int has_printed_headers=0;
  75.   int has_printed_reply=0;
  76.   while (1)
  77.   {
  78.     int st=get.run();
  79.     if (st<0)
  80.     {
  81.       printf("HTTPGet error: %s\n",get.geterrorstr());
  82.       break;
  83.     }
  84.     if (get.get_status()>0)
  85.     {
  86.       if (!has_printed_reply)
  87.       {
  88.         has_printed_reply=1;
  89.         printf("reply: %s (code:%d)\n",get.getreply(),get.getreplycode());
  90.       }
  91.       if (get.get_status()==2)
  92.       {
  93.         int len;
  94.         if (!has_printed_headers)
  95.         {
  96.           has_printed_headers=1;
  97.           printf("headers:\n");
  98.           char *p=get.getallheaders();
  99.           while (p&&*p)
  100.           {
  101.             printf("%s\n",p);
  102.             p+=strlen(p)+1;
  103.           }
  104.         }
  105.         while ((len=get.bytes_available()) > 0)
  106.         {
  107.           char buf[4096];
  108.           if (len > 4096) len=4096;
  109.           len=get.get_bytes(buf,len);
  110.           if (len>0)fwrite(buf,len,1,fp);
  111.         }
  112.       }
  113.     }
  114.     if (st==1) // 1 means connection closed
  115.     {
  116.       printf("HTTPGet done!\n");
  117.       break;
  118.     }
  119.   }
  120.   if (fp) fclose(fp);
  121.   JNL::close_socketlib();
  122.   return 0;
  123. }
  124.  
  125. #endif
  126.  
  127.  
  128. #if (TEST_TELNET_GATEWAY)
  129.  
  130. int main()
  131. {
  132.   JNL_Connection *cons[32]={0,};
  133.   JNL_Connection *outcons[32]={0,};
  134.   char textpos[32][256];
  135.   int n_cons=0;
  136.   int states[32]={0,};
  137.   
  138.   JNL::open_socketlib();
  139.   JNL_AsyncDNS dns;
  140.   JNL_Listen l(23);
  141.   while (!l.is_error())
  142.   {
  143.     Sleep(30);
  144.     if (n_cons<32)
  145.     {
  146.       JNL_Connection *con=l.get_connect();
  147.       if (con)
  148.       {
  149.         int x;
  150.         for (x = 0; x < 32; x ++)
  151.         {
  152.           if (!cons[x])
  153.           {
  154.             cons[x]=con;
  155.             outcons[x]=0;
  156.             states[x]=0;
  157.             n_cons++;
  158.             break;
  159.           }
  160.         }
  161.       }
  162.     }
  163.     int x;
  164.     for (x = 0; x < 32; x ++)
  165.     {
  166.       if (cons[x])
  167.       {
  168.         cons[x]->run();
  169.         if (outcons[x]) outcons[x]->run();
  170.  
  171.         if (cons[x]->get_state() == JNL_Connection::STATE_ERROR || cons[x]->get_state()==JNL_Connection::STATE_CLOSED ||
  172.             (outcons[x] && (cons[x]->get_state() == JNL_Connection::STATE_ERROR || cons[x]->get_state()==JNL_Connection::STATE_CLOSED)))
  173.         {
  174.           delete cons[x];
  175.           if (outcons[x]) delete outcons[x];
  176.           outcons[x]=0;
  177.           cons[x]=0;
  178.           states[x]=0;
  179.           n_cons--;
  180.         }
  181.         else
  182.         {
  183.           if (states[x]==0)
  184.           {
  185.             cons[x]->send_string("\r\nwelcome ");
  186.             states[x]++;
  187.           }
  188.           if (states[x]==1)
  189.           {
  190.             char hoststr[256];
  191.             int ret=dns.reverse(cons[x]->get_remote(),hoststr);
  192.             if (ret==0)
  193.             {
  194.               cons[x]->send_string(hoststr);
  195.               cons[x]->send_string(". host: ");
  196.               states[x]++;
  197.               textpos[x][0]=0;
  198.             }
  199.             if (ret==-1)
  200.             {
  201.               JNL::addr_to_ipstr(cons[x]->get_remote(),hoststr,256);
  202.               cons[x]->send_string(hoststr);
  203.               cons[x]->send_string(". host: ");
  204.               states[x]++;
  205.               textpos[x][0]=0;
  206.             }
  207.           }
  208.           if (states[x]==2)
  209.           {
  210.             char b;
  211.             while (cons[x]->recv_bytes(&b,1) && states[x]==2)
  212.             {
  213.               if (b == '\r' || b == '\n')
  214.               {
  215.                 if (strlen(textpos[x]))
  216.                 {
  217.                   char *p=strstr(textpos[x],":");
  218.                   int port=23;
  219.                   if (p) 
  220.                   {
  221.                     *p++=0;
  222.                     if (atoi(p)) port=atoi(p);
  223.                   }
  224.                   outcons[x]=new JNL_Connection(&dns);
  225.                   outcons[x]->connect(textpos[x],port);
  226.  
  227.                   char str[512];
  228.                   sprintf(str,"\r\nconnecting to port %d of %s\r\n",port,textpos[x]);
  229.                   cons[x]->send_string(str);
  230.                   states[x]++;
  231.                 }
  232.                 else states[x]=0;
  233.               }
  234.               else if (b == '\b') 
  235.               {
  236.                 if (textpos[x][0])
  237.                 {
  238.                   textpos[x][strlen(textpos[x])-1]=0;
  239.                   cons[x]->send_string("\b \b");
  240.               }
  241.               }
  242.               else
  243.               {
  244.                 textpos[x][strlen(textpos[x])+1]=0;
  245.                 textpos[x][strlen(textpos[x])]=b;
  246.                 cons[x]->send(&b,1);
  247.               }
  248.             }
  249.           }
  250.           if (states[x]==3)
  251.           {
  252.             char buf[1024];
  253.             outcons[x]->run();
  254.             int l=cons[x]->recv_bytes(buf,1024);
  255.             if (l) outcons[x]->send(buf,l);           
  256.             l=outcons[x]->recv_bytes(buf,1024);
  257.             if (l) cons[x]->send(buf,l);                     
  258.           }
  259.         }
  260.       }
  261.     }
  262.   }
  263.   JNL::close_socketlib();
  264.   return 0;
  265. }
  266.  
  267.  
  268. #endif
  269.  
  270. #if (TEST_LISTEN)
  271.  
  272. int main()
  273. {
  274.   JNL_HTTPServ *cons[32]={0,};
  275.   char *contents[32]={0,};
  276.   int n_cons=0;
  277.   
  278.   JNL::open_socketlib();
  279.   JNL_AsyncDNS dns;
  280.   JNL_Listen l(8000);
  281.   while (!l.is_error())
  282.   {
  283.     Sleep(100);
  284.     if (n_cons<32)
  285.     {
  286.       JNL_Connection *con=l.get_connect();
  287.       if (con)
  288.       {
  289.         int x;
  290.         for (x = 0; x < 32; x ++)
  291.         {
  292.           if (!cons[x])
  293.           {
  294.             cons[x]=new JNL_HTTPServ(con);
  295.             n_cons++;
  296.             break;
  297.           }
  298.         }
  299.       }
  300.     }
  301.     int x;
  302.     for (x = 0; x < 32; x ++)
  303.     {
  304.       if (cons[x])
  305.       {
  306.         int r=cons[x]->run();
  307.         if (r == -1 || r == 4)
  308.         {
  309.           if (r == -1) printf("error:%s\n",cons[x]->geterrorstr());
  310.           delete cons[x];
  311.           cons[x]=0;
  312.           free(contents[x]);
  313.           contents[x]=0;
  314.           n_cons--;
  315.         }
  316.         if (r == 2)
  317.         {
  318.           cons[x]->set_reply_string("HTTP/1.1 200 OK");
  319.           cons[x]->set_reply_header("Content-type:text/plain");
  320.           cons[x]->set_reply_header("Server:JNLTest");
  321.           contents[x]=(char*)malloc(32768);
  322.           char *poop=cons[x]->get_request_parm("poop");
  323.           sprintf(contents[x],"test, sucka\r\n%s\r\n\r\n",poop?poop:"no poop");
  324.           cons[x]->send_reply();
  325.         }
  326.         if (r == 3)
  327.         {
  328.           if (contents[x] && cons[x]->bytes_cansend()>strlen(contents[x]))
  329.           {
  330.             cons[x]->write_bytes(contents[x],strlen(contents[x]));
  331.             cons[x]->close(0);
  332.             free(contents[x]);
  333.             contents[x]=0;
  334.           }
  335.         }
  336.       }
  337.     }
  338.   }
  339.   JNL::close_socketlib();
  340.   return 0;
  341. }
  342.  
  343. #endif
  344.  
  345. #if (TEST_CONNECTION)
  346. int main()
  347. {
  348.   JNL::open_socketlib();
  349.   {
  350.     JNL_AsyncDNS dns;
  351.     JNL_Connection con(&dns);
  352.     con.connect("localhost",80);
  353.     FILE *fp=fopen("c:\\hi.raw","wb");
  354.     while (1)
  355.     {
  356.       con.run();
  357.       if (con.get_state()==JNL_Connection::STATE_ERROR)
  358.       {
  359.         printf("error %s\n",con.get_errstr());
  360.       }
  361.       if (con.get_state()==JNL_Connection::STATE_CLOSED)
  362.       {
  363.       }
  364.       while (con.recv_bytes_available()>0)
  365.       {
  366.         char buf[1024];
  367.         int a=con.recv_bytes_available();
  368.         if (a > 1024) a=1024;
  369.         con.recv_bytes(buf,a);
  370.         fwrite(buf,a,1,fp);
  371.       }
  372.     }
  373.     if (fp) fclose(fp);
  374.   }
  375.   JNL::close_socketlib();
  376.   return 0;
  377. }
  378. #endif
  379.  
  380. #if (TEST_ASYNCDNS)
  381. int main() 
  382. {
  383.   JNL_AsyncDNS dns;
  384.   char *hosts[]=
  385.   { 
  386.     "www.firehose.net",
  387.     "gnutella.com",
  388.     "207.48.52.200",
  389.     "www.slashdot.org",
  390.     "www.google.com",
  391.     "www.winamp.com",
  392.     "www.genekan.com",
  393.   };
  394.   char *reverses[]=
  395.   {
  396.     "64.0.160.98",
  397.     "205.188.245.120",
  398.     "207.48.52.222",
  399.     "207.48.52.200",
  400.   };
  401.   int n=0;
  402.   int pass=0;
  403.   while (n<sizeof(hosts)/sizeof(hosts[0])+sizeof(reverses)/sizeof(reverses[0]))
  404.   {
  405.     int x;
  406.     n=0;
  407.     printf("pass %d\n",pass++);
  408.     for (x = 0; x < sizeof(hosts)/sizeof(hosts[0]); x ++)
  409.     {
  410.       unsigned long addr;
  411.       printf("%-30s",hosts[x]);
  412.       switch (dns.resolve(hosts[x],&addr))
  413.       {
  414.         case 0: 
  415.           {
  416.             char str[256];
  417.             JNL::addr_to_ipstr(addr,str,256);
  418.             printf("%s\n",str); 
  419.             n++; 
  420.           }
  421.           break;
  422.         case 1: printf("looking up\n"); break;
  423.         case -1: printf("error\n"); n++; break;
  424.       }
  425.     }
  426.     for (x = 0; x < sizeof(reverses)/sizeof(reverses[0]); x ++)
  427.     {
  428.       printf("reverse: %-21s",reverses[x]);
  429.       char hn[512];
  430.       switch (dns.reverse(JNL::ipstr_to_addr(reverses[x]),hn))
  431.       {
  432.         case 0: printf("%s\n",hn); n++; break;
  433.         case 1: printf("looking up\n"); break;
  434.         case -1: printf("error\n"); n++; break;
  435.       }
  436.     }
  437.     Sleep(100);
  438.   }
  439.   return 0;
  440. };
  441. #endif
  442.  
  443. #if (TEST_UDP)
  444. int main(int argc, char **argv)
  445. {
  446.   if (argc != 2)
  447.   {
  448.     printf("usage: udptest <mode>\n");
  449.     printf("mode: 0 for client, 1 for server\n");
  450.     exit(0);
  451.   }
  452.   int mode=atoi(argv[1]);
  453.  
  454.   JNL::open_socketlib();
  455.  
  456.   switch(mode)
  457.   {
  458.   case 0:   // client mode
  459.     {
  460.       JNL_AsyncDNS dns;
  461.       JNL_UDPConnection con(0,&dns); // 0 chooses a random port
  462.       con.setpeer("localhost",80);
  463.       printf("Sending message...\n");
  464.       con.send("blah",5);
  465.       while (1)
  466.       {
  467.         con.run();
  468.         if (con.get_state()==JNL_UDPConnection::STATE_ERROR)
  469.         {
  470.           printf("error %s\n",con.get_errstr());
  471.         }
  472.         while (con.recv_bytes_available()>0)
  473.         {
  474.           char buf[1024];
  475.           int s=min(con.recv_bytes_available(), sizeof(buf));
  476.           con.recv_bytes(buf,s);
  477.           printf("received message: %s\n", buf);
  478.         }
  479.       }
  480.     }
  481.     break;
  482.   case 1:   // server (listening) mode
  483.     {
  484.       JNL_UDPConnection con(80);
  485.       printf("Waiting for messages...\n");
  486.       while(1)
  487.       {
  488.         con.run();
  489.         if (con.get_state()==JNL_UDPConnection::STATE_ERROR)
  490.         {
  491.           printf("error %s\n",con.get_errstr());
  492.         }
  493.         while (con.recv_bytes_available()>0)
  494.         {
  495.           char buf[1024];
  496.           int s=min(con.recv_bytes_available(), sizeof(buf));
  497.           con.recv_bytes(buf,s);
  498.           printf("message received: %s. Replying...\n", buf);
  499.           // reply on the addr:port from sender
  500.           struct sockaddr from;
  501.           con.get_last_recv_msg_addr(&from);
  502.           con.setpeer(&from);
  503.           con.send("blorp",6);
  504.         }
  505.       }
  506.     }
  507.     break;
  508.   }
  509.  
  510.   JNL::close_socketlib();
  511.   return 0;
  512. }
  513. #endif
  514.